package com.qianwei.footpath.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.baselib.mvp.MvpBasePresenter;
import com.common.baselib.ui.ToastUtil;
import com.qianwei.footpath.R;
import com.qianwei.footpath.base.BaseActivity;
import com.qianwei.footpath.manage.DataManager;
import com.qianwei.footpath.manage.LocalCacheDataManage;
import com.qianwei.footpath.manage.SerialPortManager;
import com.qianwei.footpath.manage.TimerManager;
import com.qianwei.footpath.utils.TextToSpeechUtils;

import java.util.TimerTask;

public class TestInfo extends BaseActivity {
    private static final String TAG = "Test---";
    private ImageView ivNan, ivNv;
    private EditText etAge;
    private int sex;
    private TimerTask timerTask;
    private TextView tvDown;
    private int testDownLoadTime = 30;
    private TextToSpeechUtils ttsUtils;

    @Override
    protected int layoutId() {
        return R.layout.info_activity;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tvDown = findViewById(R.id.tvDown);
        ivNan = findViewById(R.id.ivNan);
        ivNv = findViewById(R.id.ivNv);
        etAge = findViewById(R.id.etAge);
        ivNan.setOnClickListener(v -> {
            sex = 1;
            ivNan.setImageResource(R.drawable.nan_select);
            ivNv.setImageResource(R.drawable.nv);
            DataManager.getInstance().setSex(sex);
        });
        ivNv.setOnClickListener(v -> {
            sex = 2;
            ivNv.setImageResource(R.drawable.nv_select);
            ivNan.setImageResource(R.drawable.nan);
            DataManager.getInstance().setSex(sex);
        });
        findViewById(R.id.tvSure).setOnClickListener(v -> {
            if (sex == 0) {
                if (ttsUtils.isInitialized()) {
                    speak("请选择您的性别", "456");
                } else {
                    ToastUtil.showToastShort(getContext(), "请选择您的性别");
                }
                return;
            }
            String age = etAge.getText().toString();
            if (TextUtils.isEmpty(age)) {
                if (ttsUtils.isInitialized()) {
                    speak("请输入您的年龄", "789");
                } else {
                    ToastUtil.showToastShort(getContext(), "请输入您的年龄");
                }
                return;
            }
            DataManager.getInstance().setAge(Integer.parseInt(age));
            startActivity(new Intent(getContext(), TestA.class));
            finish();
        });
        testDownLoadTime = LocalCacheDataManage.getLocalConfigData().getPhysical_test_timeout();
        startTimeTask();
        initTTS();
    }

    private void initTTS() {
        ttsUtils = new TextToSpeechUtils(this, new TextToSpeechUtils.OnInitListener() {
            @Override
            public void onInitSuccess() {
                speak("请您补充性别和年龄信息", "123");
            }

            @Override
            public void onInitFailed(int errorCode) {

            }
        });

    }

    private void startTimeTask() {
        if (timerTask == null) {
            timerTask = new TimerTask() {
                private int secondsPassed = 0;

                @Override
                public void run() {
                    secondsPassed++;
                    runOnUiThread(() -> {
                        int dif = testDownLoadTime - secondsPassed;
                        tvDown.setText(dif + "s");
                    });
                    if (secondsPassed >= testDownLoadTime) {
                        finish();
                    }
                }
            };
            TimerManager.getInstance().startTimer(1000, 1000, timerTask); // 每秒执行一次
        }
    }

    private void speak(String text, String utteranceId) {
        ttsUtils.speak(text, utteranceId, null);
    }

    private void stopTimeTask(TimerTask timerTask) {
        if (timerTask != null) {
            TimerManager.getInstance().stopTimer(timerTask);
            timerTask.cancel();
        }
    }

    @Override
    protected void onDestroy() {
        if (ttsUtils != null) {
            ttsUtils.release();
            ttsUtils = null;
        }
        stopTimeTask(timerTask);
        super.onDestroy();
    }

    @Override
    protected MvpBasePresenter createPresenter() {
        return null;
    }
}
